package com.aolei.mallshop.cart.controller;

import com.aolei.mallshop.cart.service.CartService;
import com.aolei.mallshop.cart.vo.Cart;
import com.aolei.mallshop.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 查询出当前用户购物项
     * @return
     */
    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItem> getCurrentUserCartItems(){

        return cartService.getUserCartItems();
    }

    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.mallshop.com/cart.html";
    }

    /**
     * 改变购物项数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num){
        cartService.changeItemConut(skuId,num);
        return "redirect:http://cart.mallshop.com/cart.html";
    }

    /**
     * 改变购物项选中状态
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){

        cartService.checkItem(skuId,check);
        return "redirect:http://cart.mallshop.com/cart.html";

    }

    /**
     * 跳转到购物车
     * 判断是否登录，若未登录则是临时购物车，若登录，合并购物车
     * @param model
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {

        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);

        return "cartList";
    }

    //redirectAttributes.addAttribute把数据放在url,
    //redirectAttributes.addFlashAttribute()把数据放在session，只能取一次

    /**
     * 添加商品到购物车
     * @param skuId
     * @param num
     * @param redirectAttributes
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num")Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {

        cartService.addToCart(skuId,num);
        redirectAttributes.addAttribute("skuId",skuId);

        return "redirect:http://cart.mallshop.com/addToCartSuccess.html";
    }

    /**
     * 跳转到添加购物车成功页，防止重复刷新，造成商品数量一直增加
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){
        CartItem item = cartService.getCartItem(skuId);
        model.addAttribute("item",item);
        return "success";
    }

}
